﻿' Copyright © Microsoft Corporation.  All Rights Reserved.
' This code released under the terms of the 
' Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)
' <copyright file="PlaylistCollection.vb" company="Microsoft">
'     Copyright © Microsoft Corporation. All rights reserved.
' </copyright>
' <summary>Implements the PlaylistCollection class</summary>
' <author>Microsoft Expression Encoder Team</author>

Imports Microsoft.VisualBasic
Imports System.Windows.Browser
Imports System.Xml
Imports System.Windows
Imports System.Collections.Specialized

Namespace ExpressionMediaPlayer
	''' <summary>
	''' This class represents a collection of media items to play.
	''' </summary>    
	<ScriptableType> _
	Public Class PlaylistCollection
		Inherits ScriptableObservableCollection(Of PlaylistItem)
		''' <summary>
		''' Inserts an item into the collection.
		''' </summary>
		''' <param name="index">Index to insert the item at.</param>
		''' <param name="item">Item to insert.</param>
		Protected Overrides Sub InsertItem(ByVal index As Integer, ByVal item As PlaylistItem)
			If item IsNot Nothing Then
				item.OwnerCollection = Me
				AddHandler item.PropertyChanged, AddressOf item_PropertyChanged
				MyBase.InsertItem(index, item)
			End If
		End Sub
		''' <summary>
		''' playlist item changed
		''' </summary>
		''' <param name="sender"></param>
		''' <param name="e"></param>
		Private Sub item_PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs)
			OnCollectionChanged(New NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset))
		End Sub
		''' <summary>
		''' Removes an item at the given index.
		''' </summary>
		''' <param name="index">Index of the item to remove.</param>
		Protected Overrides Sub RemoveItem(ByVal index As Integer)
			Items(index).OwnerCollection = Nothing
			MyBase.RemoveItem(index)
		End Sub
		#Region "Serialization"
		''' <summary>
		''' top level XML node for this class
		''' </summary>
		Friend Const xmlNode As String = "Items"
		''' <summary>
		''' deserialise this object
		''' </summary>
		''' <param name="reader">XmlReader to deserialize from</param>
		''' <returns>this</returns>
		Friend Function Deserialize(ByVal reader As XmlReader) As PlaylistCollection
			If Not reader.IsStartElement(PlaylistCollection.xmlNode) Then
				Throw New InvalidPlaylistException()
			End If

			reader.Read()
			Do While Not(reader.Name = PlaylistCollection.xmlNode AndAlso reader.NodeType = XmlNodeType.EndElement)
				If reader.IsStartElement("PlaylistItem") Then
					Me.Add(New PlaylistItem().Deserialize(reader))
				ElseIf reader.IsStartElement() Then
					Throw New InvalidPlaylistException(reader.Name, PlaylistCollection.xmlNode)
				ElseIf Not reader.Read() Then
					Exit Do
				End If
			Loop
			Return Me
		End Function
		''' <summary>
		''' serialize this object
		''' </summary>
		''' <param name="writer">XmlWriter to serialze to</param>
		Friend Sub Serialize(ByVal writer As XmlWriter)
			writer.WriteStartElement(PlaylistCollection.xmlNode)
            For Each item As PlaylistItem In Me
                item.Serialize(writer)
            Next item
			writer.WriteEndElement()
		End Sub
		#End Region
	End Class
End Namespace
